package com.yanqu.road.dao.impl.activity.selectlottery;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.selectlottery.data.SelectLotteryJcUserData;
import com.yanqu.road.entity.activity.selectlottery.data.SelectLotteryUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SelectLotteryJcUserDataDaoImpl extends TempDataObjectDao<SelectLotteryJcUserData> {

    public static String getSelectLotteryJcUserDataSql = "select * from `t_u_activity_select_lottery_jc_user_data` where `userId`=?";
    public Map<Integer, Map<Integer,SelectLotteryJcUserData>> getSelectLotteryJcUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Integer, Map<Integer,SelectLotteryJcUserData>> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSelectLotteryJcUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                SelectLotteryJcUserData data = parse(rs);
                Map<Integer,SelectLotteryJcUserData> smallTypeMap = map.computeIfAbsent(data.getActivityType(), k -> new ConcurrentHashMap<>());
                smallTypeMap.put(data.getActivitySmallType(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String updateSelectLotteryJcUserDataSql = "update `t_u_activity_select_lottery_jc_user_data` set drawNum=?,seRewardNum=?,createTime=? where userId=? and activityType=? and activitySmallType=?";
    @Override
    protected boolean updateData(SelectLotteryJcUserData selectLotteryJcUserData) {
        selectLotteryJcUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryJcUserData.getDrawNum()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryJcUserData.getSeRewardNum()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryJcUserData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryJcUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryJcUserData.getActivityType()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryJcUserData.getActivitySmallType()));
        if (getDb().executeNoneQuery(updateSelectLotteryJcUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSelectLotteryJcUserDataSql = "insert into `t_u_activity_select_lottery_jc_user_data`(userId,activityType,activitySmallType,drawNum,seRewardNum,createTime) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(SelectLotteryJcUserData selectLotteryJcUserData) {
        selectLotteryJcUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryJcUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryJcUserData.getActivityType()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryJcUserData.getActivitySmallType()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryJcUserData.getDrawNum()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryJcUserData.getSeRewardNum()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryJcUserData.getCreateTime()));
        if (getDb().executeNoneQuery(insertSelectLotteryJcUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SelectLotteryJcUserData parseData(ResultSet rs) throws SQLException {
        SelectLotteryJcUserData selectLotteryJcUserData = new SelectLotteryJcUserData();
        selectLotteryJcUserData.setUserId(rs.getLong("userId"));
        selectLotteryJcUserData.setActivityType(rs.getInt("activityType"));
        selectLotteryJcUserData.setActivitySmallType(rs.getInt("activitySmallType"));
        selectLotteryJcUserData.setDrawNum(rs.getInt("drawNum"));
        selectLotteryJcUserData.setSeRewardNum(rs.getInt("seRewardNum"));
        selectLotteryJcUserData.setCreateTime(rs.getLong("createTime"));
        return selectLotteryJcUserData;
    }
}
